package cn.agiledata.bank.transaction.service;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.agiledata.bank.hibernateBean.ScoresHis;
import cn.agiledata.bank.hibernateBean.ScoresRules;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.scores.service.IScoresService;
import cn.agiledata.bank.common.dao.SysTemplate;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.service.email.FillMoneyEmail;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class TransactionAbundance extends TransactionFactoryAbstract{	

	private SysTemplate sysTemplate ;
	private IScoresService scoresService;

	public void setSysTemplate(SysTemplate sysTemplate) {
		this.sysTemplate = sysTemplate;
	}


	/* (non-Javadoc)
	 * @see cn.agiledata.bank.impl.TransactionFactory#saveTransaction(cn.agiledata.bank.hibernateBean.Transaction)
	 */
	public synchronized void saveTransaction(Transaction tran){		
		//获得具体收费模式
		SysParameter feeMode = sysTemplate.getFeeMode(tran.getCredit().getId());
		if (tran.getInsideStatus().equals(Constant.INSIDE_STATUS_YES)){
			List bankList = ftd.fillAbundanceTransaction(tran, feeMode);
			getHibernateTemplate().saveOrUpdateAll(bankList);
			getHibernateTemplate().update(tran.getCredit());
			ahoi.updateAll(tran.getTransDetails().toArray());
			ahoi.update(tran.getCredit(),tran.getAmount(),tran.getCreditFee(),Constant.RECEIVE,tran
					,tran.getDebit().getId());
			getHibernateTemplate().flush();
			ScoresHis sh = new ScoresHis();
			if (tran.getCredit().getAccount().getAccountType().equals(Constant.ACCOUNT_TYPE_BUYER)
					&& Constant.ACTION_TYPE_A.equals(tran.getActionType().getId().toString())) {
				ScoresRules sr = scoresService.getScoresRule(Constant.SCORES_SOURCE_D, 
						tran.getCredit().getAccount().getId().toString());
				if(sr!=null){
					sh=scoresService.saveScoresHistory(tran.getCredit().getAccount().getId().toString(), 
						sr, tran, null);
				}
			}
				
			FillMoneyEmail mail = (FillMoneyEmail) IPBAppFactory.getObj("fillMoneyMail");
				TransactionTemplate template = (TransactionTemplate) IPBAppFactory.getObj("transactionTemplate");
				String now = DateUtil.date2String(new Date());
				if(template.ifSendMail(tran.getCredit().getAccount(), Constant.FILLMONEY)){	
					if(tran.getActionType().getId().toString().equals(Constant.ACTION_TYPE_A)){
						mail.setTitlemessage( " 您于"+now+"进行了网上银行充值：");
						mail.setVmFile("mail-fillMoneybank.vm");
						mail.setEmailTitle("您的账户充值通知");
						mail.sendMail(tran, sh);
					}
				}
				
				if(template.ifSendMail(tran.getCredit().getAccount(), Constant.GAIN_BY_TOOL)){
						if(tran.getActionType().getId().toString().equals(Constant.ACTION_TYPE_F)){
							// 非个人帐户付款Email
							if (StringUtils.isNotBlank(tran.getPersonName()))
								mail.setTitlemessage( " 您于"+now+"收到了"+tran.getPersonName()+"通过支付链支付给您的一笔款项：");
							else
								mail.setTitlemessage( " 您于"+now+"收到了通过支付链支付给您的一笔款项：");
							mail.setVmFile("mail-vnvTransaction.vm");
							mail.setEmailTitle("支付链收款成功通知");
							mail.sendMail(tran, sh);
						}
				}
				
				if(template.ifSendMobile(tran.getCredit().getAccount(), Constant.FILLMONEY)){
					if(tran.getActionType().getId().toString().equals(Constant.ACTION_TYPE_A)){
						// 网上银行充值
						mail.setSmsContext("sms-fillMoney.vm");
						mail.sendSms(tran,sh);
					}
				}
				
				if(template.ifSendMobile(tran.getCredit().getAccount(), Constant.GAIN_BY_TOOL)){	
					if(tran.getActionType().getId().toString().equals(Constant.ACTION_TYPE_F)){
						// 支付链
						mail.setSmsContext("sms-bankToVnv.vm");
						mail.sendSms(tran,sh);
					}
				}
		}
		if(Constant.ACTION_TYPE_X.equals(tran.getActionType().getId().toString())) {//如果是卡转入
			tran.setDebitFee(new Double(0.0));
		}
		getHibernateTemplate().saveOrUpdate(tran);
		ahoi.updateTranHis(tran);
	}
	public void setScoresService(IScoresService scoresService) {
		this.scoresService = scoresService;
	}




}